package com.wangsd.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Map;
import java.util.Set;

/**
 * MD5加密工具类
 */
@Slf4j
public class MD5Utils {

    public static String getMD5Sign(Map<String, String> map, String key, int type) {
        StringBuffer sb = new StringBuffer();
        Set<Map.Entry<String, String>> entry = map.entrySet();
        String signStr = null;
        if (type == 1) { /** 签名串 a=1&b=2&key=abc (小写)*/
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb.append("key=" + key);
            signStr = DigestUtils.md5Hex(sb.toString()).toLowerCase();
        } else if (type == 2) { /** 签名串 a=1&b=2&key=abc (大写)*/
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb.append("key=" + key);
            signStr = DigestUtils.md5Hex(sb.toString()).toUpperCase();
        } else if (type == 3) { /** 签名串 a=1&b=2abc (小写)*/
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb = sb.replace(sb.length() - 1, sb.length(), "");
            sb.append(key);
            signStr = DigestUtils.md5Hex(sb.toString()).toLowerCase();
        } else if (type == 4) { /** 签名串 a=1&b=2&abc (大写)*/
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb.append(key);
            signStr = DigestUtils.md5Hex(sb.toString()).toUpperCase();
        } else if (type == 5) { /** 签名串 a=1|b=2|abc (大写)*/
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getValue() + "|");
            }
            sb.append(key);
            signStr = DigestUtils.md5Hex(sb.toString()).toUpperCase();
        }
        log.debug("签名串：" + sb.toString() + " 签名结果：" + signStr);
        return signStr;
    }

    public static String getSha256Sign(Map map, String key, int type) {
        StringBuffer sb = new StringBuffer();
        Set<Map.Entry<String, String>> entry = map.entrySet();
        String signStr = null;
        if (type == 1) { /** 签名串 a=1&b=2&key=abc (小写)*/
            for (Map.Entry<String, String> temp : entry) {
                sb.append(temp.getKey() + "=" + temp.getValue() + "&");
            }
            sb = sb.replace(sb.length() - 1, sb.length(), "");
            sb.append(key);
            signStr = DigestUtils.sha256Hex(sb.toString());
        }
        log.debug("签名串：" + sb + " 签名结果：" + signStr);
        return signStr.toString();
    }

    public static void main(String[] args) throws Exception {
//        System.out.println(DigestUtils.md5Hex("12"));
        String value = "out_request_no0=15340726911578389081362&send_coupon_merchant=1559989741&stock_id0=1218400000000011";
        String key = "047oaGf9ZGdTMumCoBHOt7AdInFTnxSu";
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
        sha256_HMAC.init(secret_key);
        byte[] array = sha256_HMAC.doFinal(value.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        System.out.println(sb.toString().toUpperCase());
    }
}
